<?php

namespace app\common\service\DecorateService;
use app\BaseService;
use app\model\BbcModel\Decorate;
use app\Normal;
use app\model\BbcModel\DecorateTab;
use app\model\BbcModel\DecoratePicture;
use app\common\service\DecorateService\DecorateTabService;

class DecorateService extends BaseService
{
    public function templateInfo(array $param): array{
        if(!isset($param['type']) || !in_array($param['type'],[1,2])){
            return Normal::returnCode(Normal::FAIL,'类型参数错误');
        }
        $tab_id_lists = Decorate::instance()->where(['cid'=>$param['cid'],'type'=>$param['type']])->value('tab_id');
        if(!$tab_id_lists){ //未配置过tab页
            return Normal::returnCode(Normal::FAIL,'未配置装修');
        }else{
            $tab_lists = DecorateTab::instance()->where('id','in',$tab_id_lists)->order('show_order asc')->field('id,name')->select()->toArray();
            $template_info = $tab_lists;
        }
        return Normal::returnCode(Normal::SUC,'获取成功',$template_info);

    }

    public function setTemplate( array $param): array{
        if(!isset($param['tab_id']) || !$param['tab_id']){
            return Normal::returnCode(Normal::FAIL,'微页面ID不能为空');
        }
        if(!isset($param['type']) || !in_array($param['type'],[1,2])){
            return Normal::returnCode(Normal::FAIL,'类型参数错误');
        }
        $tab = DecorateTab::instance()->where(['id'=>$param['tab_id'],'cid'=>$param['cid'],'df'=>0])->find();
        if(!$tab){
            return Normal::returnCode(Normal::FAIL,'微页面不存在');
        }
        if($tab['release_status'] != 1){
            return Normal::returnCode(Normal::FAIL,'此微页面还未发布');
        }
        $template_info = Decorate::instance()->where(['cid'=>$param['cid'],'type'=>$param['type']])->field('id,name,tab_id')->find();
        if($template_info){ //已设置过
            /*$tab_ids = explode(',',$template_info['tab_id']);
            if(!in_array($param['tab_id'],$tab_ids)){ //当前微页面未设置
                $tab_ids[] = $param['tab_id'];
            }
            $tab_ids_str = implode(',',$tab_ids);*/
            $res = Decorate::instance()->where(['id'=>$template_info['id']])->update(['tab_id'=>$param['tab_id'],'md'=>time()]);
        }else{
//            $tab_ids              = [$param['tab_id']];
//            $tab_ids_str          = implode(',', $tab_ids);

            $insertData['cid']    = $param['cid'];
            $insertData['type']   = $param['type'];
            $insertData['tab_id'] = $param['tab_id'];
            $insertData['cd']     = time();
            $insertData['md']     = time();
            $insertData['name']   = '首页';
            $res = Decorate::instance()->insert($insertData);
        }
        if($res == false){
            return Normal::returnCode(Normal::FAIL,'更换失败');
        }
        return Normal::returnCode(Normal::SUC,'更换成功');
    }

    public function pictureLists( array $param): array{
        $page = (isset($param['page']) && $param['page'] != '') ? $param['page'] : 1;
        $limit = (isset($param['limit']) && $param['limit'] != '') ? $param['limit'] : 20;
        $data_lists = DecoratePicture::instance()->where(['cid'=>$param['cid'],'df'=>0])->page((int)$page)->limit((int)$limit)->field('id,name,img_url')->select()->toArray();
        $total = DecoratePicture::instance()->where(['cid'=>$param['cid'],'df'=>0])->count();
        return Normal::returnCode(Normal::SUC,'获取成功',['list'=>$data_lists,'total'=>$total]);
    }

    public function addPicture( array $param): array{
        if(!isset($param['name']) || !$param['name']){
            return Normal::returnCode(Normal::FAIL,'名称不能为空');
        }
        if(!isset($param['img_url']) || !$param['img_url']){
            return Normal::returnCode(Normal::FAIL,'图片地址不能为空');
        }
        $param['name'] = trim($param['name']);
        if(DecoratePicture::instance()->info(['cid'=>$param['cid'],'name'=>$param['name'],'df'=>0])){
            return Normal::returnCode(Normal::FAIL,'名称已存在');
        }
        $insertData['cid']     = $param['cid'];
        $insertData['cb_id']   = $param['cb_id'];
        $insertData['md']      = time();
        $insertData['cd']      = time();
        $insertData['name']    = $param['name'];
        $insertData['img_url'] = $param['img_url'];
        $res = DecoratePicture::instance()->insert($insertData);
        if($res == false){
            return Normal::returnCode(Normal::FAIL,'新增失败');
        }
        return Normal::returnCode(Normal::SUC,'新增成功');
    }

    public function editPicture( array $param): array{
        if(!isset($param['id']) || !$param['id']){
            return Normal::returnCode(Normal::FAIL,'缺少ID');
        }
        if(!isset($param['name']) || !$param['name']){
            return Normal::returnCode(Normal::FAIL,'名称不能为空');
        }
        if(!isset($param['img_url']) || !$param['img_url']){
            return Normal::returnCode(Normal::FAIL,'图片地址不能为空');
        }
        $info = DecoratePicture::instance()->where(['cid'=>$param['cid'],'df'=>0])->find();
        if(!$info){
            return Normal::returnCode(Normal::FAIL,'数据不存在');
        }
        $res = DecoratePicture::instance()->where(['id'=>$param['id']])->update(['name'=>$param['name'],'img_url'=>$param['img_url'],'md'=>time()]);
        if($res == false){
            return Normal::returnCode(Normal::FAIL,'编辑失败');
        }
        return Normal::returnCode(Normal::SUC,'编辑成功');
    }

    public function delPicture( array $param): array{
        if(!isset($param['id']) || !$param['id']){
            return Normal::returnCode(Normal::FAIL,'缺少ID');
        }
        $info = DecoratePicture::instance()->where(['cid'=>$param['cid'],'df'=>0])->find();
        if(!$info){
            return Normal::returnCode(Normal::FAIL,'数据不存在');
        }
        $r = DecorateTabService::instance()->checkData($param);
        if(in_array($param['id'],$r['long_img'])){
            return Normal::returnCode(Normal::FAIL,'长图被使用无法删除');
        }
        $res = DecoratePicture::instance()->where(['id'=>$param['id']])->update(['df'=>1,'md'=>time()]);
        if($res == false){
            return Normal::returnCode(Normal::FAIL,'删除失败');
        }
        return Normal::returnCode(Normal::SUC,'删除成功');
    }
}